package com.ilock.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ilock.bean.Ilockinfo;
import com.ilock.bean.Iuser;
import com.ilock.bean.Templockinfo;
import com.ilock.service.ILockService;
import com.ilock.service.IUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LockBindAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ILockService ILockService;
	private IUserService IUserService;
	private Map<String, Object> bindMap;
	private List lockList;
	private Ilockinfo Ilockinfo;
	private String useremail;
	private String userpassword;
	private String lockpassword;
	
	
	public String getUseremail() {
		return useremail;
	}


	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}


	public String getUserpassword() {
		return userpassword;
	}


	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}


	public String getLockpassword() {
		return lockpassword;
	}


	public void setLockpassword(String lockpassword) {
		this.lockpassword = lockpassword;
	}


	public void setILockService(ILockService ILockService) {
		this.ILockService = ILockService;
	}

	
	public void setIUserService(IUserService iuserservice) {
		this.IUserService = iuserservice;
	}
	
	
	public Ilockinfo getIlockinfo() {
		return Ilockinfo;
	}


	public void setIlockinfo(Ilockinfo ilockinfo) {
		Ilockinfo = ilockinfo;
	}


	public Map getBindMap() {
		return bindMap;
	}

	public void setBindMap(Map bindMap) {
		this.bindMap = bindMap;
	}
	
	

	public List getLockList() {
		return lockList;
	}


	public void setLockList(List lockList) {
		this.lockList = lockList;
	}


	public String showLock()throws Exception{
		Iuser iuser=checkUser();
		lockList = new ArrayList();
		if(iuser!=null){
		this.setLockList(ILockService.getUserLockList(iuser.getIuserNumber()));
		}
		return "showlock";
	}
	
	public String deleteLock()throws Exception{
		Iuser iuser=checkUser();
		lockList = new ArrayList();
		Map<String,Object> tempmap = new HashMap<String, Object>();
		if(iuser!=null){
			ILockService.deleteOwnLock(iuser.getIuserNumber());
			tempmap.put("state", "delete success");			
		}else{
			tempmap.put("state", "user info error");
		}
		this.setBindMap(tempmap);
		return "showlock";
		
	}
	public String bindCheck()throws Exception{
		Map<String,Object> map  = new HashMap<String,Object>();
		Iuser iuser=checkUser();
		System.out.println("enter bindcheck");
		if(iuser!=null){
			Templockinfo templockinfo = ILockService.findTemplockInfo(iuser.getIuserNumber());
			lockpassword = transferToken(lockpassword);
			if(lockpassword!=null && templockinfo!=null &&templockinfo.getTempPassword().equals(lockpassword)){
				map = ILockService.bindLock(iuser.getIuserNumber(),templockinfo.getIlockInfoNumber(),lockpassword);
			}else{
				map.put("state", "bindFail with wrong info");
			}
		}else{
			map.put("state", "userCheckFail");
			
		}
		this.setBindMap(map);
		return "success";
	}
	
	public String execute()throws Exception{
		Map<String,Object> map  = new HashMap<String,Object>();
		Iuser iuser = checkUser();
		if(checkUser()!=null){
			ILockService.deleteTempLockInfo(iuser.getIuserNumber());
			lockpassword = transferToken(lockpassword);
			Ilockinfo ilockinfo = ILockService.getNewToken(lockpassword);
			System.out.println("step1");
			if(ilockinfo!=null){
				ILockService.insertTempLockInfo(iuser.getIuserNumber(),ilockinfo.getIlockInfoNumber(), ilockinfo.getIlockInfoPassword());
				map.put("templockinfo", ilockinfo);
				map.put("state", "bindConfirm");
				System.out.println("bindConfirm");
				System.out.println("step2");
			}else{
				System.out.println("bindFail with wrong info");
				map.put("state", "bindFail with wrong info");
			}
		}else{
				System.out.println("userCheckFail");
				map.put("state", "userCheckFail");
		}
		this.setBindMap(map);
		return "success";
	}
	
	/**
	 * check user's info
	 * @return
	 */
	public Iuser checkUser(){
		Iuser iuser = null;
		iuser=IUserService.checkIn(useremail, userpassword);
		return iuser;
	}
	
	/**
	 * fill token's space with '+'
	 * @param oldtoken
	 * @return
	 */
	public String transferToken(String oldtoken){
		if(oldtoken.indexOf(" ")!=-1){
			oldtoken = lockpassword.replace(" ", "+");	
		}
		return oldtoken;
	}

}
